//
//  UIColor+Hex.m
//  YuxiCloud
//
//  Created by LIUKUN-PC on 16/4/14.
//  Copyright © 2016年 ynnissi. All rights reserved.
//

#import "ZXCategories.h"
#import <objc/runtime.h>

@implementation UIColor (Hex)

+ (UIColor *)colorWithHex:(NSInteger)hexColor alpha:(CGFloat)alpha{
    return [UIColor colorWithRed:((hexColor>>16)&0xFF)/255.0
                        green:((hexColor>>8)&0xFF)/255.0
                        blue:(hexColor&0xFF)/255.0 alpha:alpha];
    

}

+ (UIColor *)colorWithHex:(NSInteger)hexColor {
    return [self colorWithHex:hexColor alpha:1];
}


@end

@implementation UIImage (Color)

+ (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end

@implementation UIViewController (StoryboardID)

+ (NSString *)storyboardID {
    return NSStringFromClass(self.class);
}

+ (instancetype)viewControllerFromMainStoryboard {
    return [self viewControllerFromStoryboadName:nil];
}

+ (instancetype)viewControllerFromStoryboadName:(NSString *)storyboardName {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName == nil ? @"Main" : storyboardName bundle:nil];
    return [self viewControllerFromStoryboad:storyboard];
}

+ (instancetype)viewControllerFromStoryboad:(UIStoryboard *)storyboard {
    return [storyboard instantiateViewControllerWithIdentifier:[self storyboardID]];
}


@end


@implementation UITableViewCell (ReuseID)

+ (NSString *)reuseID {
    return NSStringFromClass(self.class);
}

@end

@implementation UICollectionViewCell (ReuseID)

+ (NSString *)reuseID {
    return NSStringFromClass(self.class);
}

@end

@implementation UIView (Storyboard)

- (CGFloat)borderWidth {
    return self.layer.borderWidth;
}

- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
    
}

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    self.layer.borderWidth = borderWidth;
}

- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0;
}

@end

@interface UIViewController (AlertViewPrivate)

@property (nonatomic, strong) UIAlertAction *lk_alert_view_OkAction;

@end


@implementation UIViewController (AlertView)

- (UIAlertController *)showAlertViewWithMessage:(NSString *)message cancelHandler:(void (^)())cancelBlock {
    return [self showAlertViewWithTitle:@"提示" message:message cancelHandler:cancelBlock];
}

- (UIAlertController *)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelHandler:(void (^)())cancelBlock {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (cancelBlock) {
            cancelBlock();
        }
    }];
    [alertController addAction:action];
    
    [self presentViewController:alertController animated:YES completion:nil];
    return alertController;
}

- (UIAlertController *)showTextFieldAlertViewWithTitle:(NSString *)title message:(NSString *)message completion:(void (^)(NSString *))completion {
    return [self showTextFieldAlertViewWithTitle:title message:message configurationHandler:nil completion:completion];
}

- (UIAlertController *)showTextFieldAlertViewWithTitle:(NSString *)title message:(NSString *)message configurationHandler:(void (^)(UITextField *))configurationHandler completion:(void (^)(NSString *))completion {
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lk_alert_view_textFieldDidChangeAction:) name:UITextFieldTextDidChangeNotification object:textField];
        
        if (configurationHandler) {
            configurationHandler(textField);
        }
        
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
        
        if (completion) {
            completion(alertController.textFields.firstObject.text);
        }
    }];
    
    okAction.enabled = alertController.textFields.firstObject.text.length > 0;
    
    self.lk_alert_view_OkAction = okAction;
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    return alertController;
    
}

- (void)lk_alert_view_textFieldDidChangeAction:(NSNotification *)notification {
    
    UITextField *textField = notification.object;
    
    self.lk_alert_view_OkAction.enabled = textField.text.length > 0;

}

- (UIAlertAction *)lk_alert_view_OkAction {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setLk_alert_view_OkAction:(UIAlertAction *)lk_alert_view_OkAction {
    
    objc_setAssociatedObject(self, @selector(lk_alert_view_OkAction), lk_alert_view_OkAction, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

@implementation UITextField (TextIndent)

- (void)addTextIndent:(CGFloat)indent {
    [self addTextIndentViewWithFrame:CGRectMake(0, 0, indent, 5)];
}

- (void)addTextIndentViewWithFrame:(CGRect)indentFrame {
    self.leftView = [[UIView alloc] initWithFrame:indentFrame];
    self.leftView.userInteractionEnabled = NO;
    self.leftViewMode = UITextFieldViewModeAlways;
}

@end
